package com.zf.nio.buffer;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * 使用mmap模型的 Buffer, 将内核缓冲区的数据通过映射，使得我们可以直接访问内核缓存区中的数据，而不用将数据从内核缓冲区复制到用户缓冲区，去除了一次cpu的拷贝操作
 * @author Zhou Feng
 */
public class MappedByteBufferDemo01 {
    public static void main(String[] args) throws IOException {
        FileChannel fileChannel = FileChannel.open(Paths.get("d:\\a.txt"), StandardOpenOption.READ, StandardOpenOption.WRITE);
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, fileChannel.size());
        mappedByteBuffer.put("map".getBytes());
        fileChannel.close();
    }
}
